package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.service.AdminStorageService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName AdminStorageServiceImpl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/7/25 11:12
 * @Version V1.0
 **/
public class AdminStorageServiceImpl implements AdminStorageService {
    @Override
    public void insert(MarketStorage marketStorage) {
        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper storageMapper = session.getMapper(MarketStorageMapper.class);
        storageMapper.insert(marketStorage);
    }

    @Override
    public List<MarketStorage> getList(String page, String limit, String key, String name, String sort, String order) {
        // 获取SlqSession
        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper storageMapper = session.getMapper(MarketStorageMapper.class);
        // 增加筛选条件
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        if (!StringUtils.isEmpty(key)) {
            criteria.andKeyEqualTo(key);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameEqualTo(name);
        }
        // 增加排序条件
        marketStorageExample.getOrderByClause(sort + " " + order);
        // 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketStorage> marketStorages = storageMapper.selectByExample(marketStorageExample);
        // 提交事务
        session.commit();
        session.close();
        // 将list返回给controller
        return marketStorages;
    }

    @Override
    public MarketStorage getData(String id, String name) {
        // 获取SqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper storageMapper = session.getMapper(MarketStorageMapper.class);

        // 更新对象数据
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setId(Integer.parseInt(id));
        marketStorage.setName(name);
        marketStorage.setUpdateTime(LocalDateTime.now());
        // 更新数据库中的数据
        storageMapper.updateByPrimaryKeySelective(marketStorage);
        // 提交事务
        session.commit();
        session.close();
        // 返回做出更改的storage对象给controller
        return marketStorage;
    }

    @Override
    public void deleteData(String id) {
        /**
         * @param id:
         * @return void
         * @author Lazarus_Liu
         * @description
         * @date 2023/7/26 20:51
         */
        // 获取SqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);
        MarketStorage marketStorage = new MarketStorage();

        // 删除
        mapper.deleteByPrimaryKey(Integer.parseInt(id));
        // 提交事务
        session.commit();
        session.close();

    }
}
